<?php

namespace App\Http\Controllers\CallingCards\OfficialWeb;

use App\Events\CallingCards\OfficialRecordEvent;
use App\Http\Controllers\BaseController;
use App\Service\CallingCards\ThirdClueService;
use Illuminate\Http\Request;

class FranchiseeController extends BaseController
{
    public function record(Request $request)
    {
        $ip        = $request->ip();
        $invitorId = (int)$request->post('invitorId', 202);
        $name      = (string)$request->name;
        $mobile    = (string)$request->mobile;
        $province  = (string)$request->province;
        $city      = (string)$request->city;
        $district  = (string)$request->district;
        $remark    = (string)$request->remark;
        event(new OfficialRecordEvent($ip, $invitorId, $name, $mobile, $province, $city, $district, $remark));
        return $this->success();
    }

    public function thirdPartyRecord(Request $request)
    {
        $this->validate($request, [
            'cardId'    => 'required|int|min:1',
            'name'      => 'required|string|min:1|max:20',
            'mobile'    => 'required|string|min:11',
            'timestamp' => 'required|int|min:1',
            'sign'      => 'required|string',
            'remark'    => 'string|max:512',
        ]);
        try {
            ThirdClueService::checkSign($request->cardId, $request->post('name', ''), $request->mobile, $request->timestamp, (string)$request->province, (string)$request->city, $request->sign, (string)$request->remark);
            event(
                new OfficialRecordEvent(
                    (string)$request->ip(),
                    (string)$request->post('cardId', ''),
                    (string)$request->post('name', ''),
                    (string)$request->post('mobile', ''),
                    (string)$request->post('province', ''),
                    (string)$request->post('city', ''),
                    (string)$request->post('district', ''),
                    (string)$request->post('remark', '')
                )
            );
        } catch (\Exception $exception) {
            return $this->fail(400, $exception->getMessage());
        }
        return $this->success();
    }
}
